<script>
    app.controller('UserListCtrl', function ($scope, $templateCache, $compile, $filter, $http, $state) {
        $('#userList').DataTable({
            ajax: {
                url: 'r/users',
                dataSrc: ''
            },
            columns: [
                {data: 'id'},
                {data: 'email'},
                {
                    searchable: false,
                    data: function (data, type, row, meta) {
                        return $filter('date')(data.createdTime, 'yyyy-MM-dd HH:mm:ss');
                    }
                },
                {
                    searchable: false,
                    orderable: false,
                    width: 68,
                    type: 'html',
                    data: function (data, type, row, meta) {
                        var tmpl = $templateCache.get('/table_basic_actions.html');
                        var editTmpl = sprintf('ui-sref="user-edit({id:%s})"', data.id);
                        var deleteTmpl = sprintf('ng-click="deleteUser(%s)"', data.id);
                        return sprintf(tmpl, editTmpl, deleteTmpl);
                    }
                }
            ],
            initComplete: function () {
                $compile($(this))($scope);
            }
        });

        $('.dataTables_length select').select2({
            minimumResultsForSearch: Infinity,
            width: 'auto'
        });

        // Highlighting rows and columns on mouseover
        dataTableHighlightColumns();

        // 删除应用
        $scope.deleteUser = function (id) {
            var param = {
                title: '确认删除用户',
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#EF5350",
                confirmButtonText: '确认',
                cancelButtonText: '取消'
            };
            swal(param, function (isConfirm) {
                if (isConfirm) {
                    $http.delete('r/users/' + id).success(function () {
                        $state.go($state.current, {}, {reload: true});
                    });
                }
            });
        }
    })
    ;
</script>
<div class="panel panel-flat" ng-controller="UserListCtrl">
    <div class="panel-heading">
        <h5 class="panel-title">用户列表</h5>
    </div>

    <div class="panel-body">
    </div>
    <table id="userList" class="table table-bordered table-hover datatable-highlight">
        <thead>
        <tr>
            <th>ID</th>
            <th>邮箱</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
    </table>
</div>
